<?php
define('CODE_MARK','code');

function createCodeImg(){
	$image = imagecreate(90, 25);
	
	imagecolorallocate($image, 255, 255, 255);
	
	$col1 = rand(1,255);
	$col2 = rand(1,255);
	$col3 = rand(1,255);
	$col4 = rand(1,255);
	$col5 = rand(1,255);
	
	$color1 = imagecolorallocate($image, $col1, $col3, $col2);
	$color2 = imagecolorallocate($image, $col5, $col2, $col4);
	$color3 = imagecolorallocate($image, $col2, $col1, $col5);
	$color4 = imagecolorallocate($image, $col3, $col4, $col1);
	
	$text1 = rand(1, 9);
	$text2 = rand(1, 9);
	$text3 = rand(1, 9);
	$text4 = rand(1, 9);
	
	$_SESSION[CODE_MARK] = $text1.$text2.$text3.$text4;
	
	$angle1 = rand(330,360);
	$angle2 = rand(1,30);
	
	imagefttext($image, 17, $angle1, 5, 20, $color1, 'C:\Windows\Fonts\msyh.ttc', $text1);
	imagefttext($image, 17, $angle2, 25, 20, $color2, 'C:\Windows\Fonts\msyh.ttc', $text2);
	imagefttext($image, 17, $angle1, 45, 20, $color3, 'C:\Windows\Fonts\msyh.ttc', $text3);
	imagefttext($image, 17, $angle2, 65, 20, $color4, 'C:\Windows\Fonts\msyh.ttc', $text4);
	
	$x1 = rand(1,80);
	$x2 = rand(1,80);
	$y1 = rand(1,25);
	$y2 = rand(1,25);
	$a1 = rand(1,80);
	$a2 = rand(1,80);
	$b1 = rand(1,25);
	$b2 = rand(1,25);
	$m1 = rand(1,80);
	$m2 = rand(1,80);
	$n1 = rand(1,25);
	$n2 = rand(1,25);
	
	imageline($image, $x1, $y1, $x2, $y2, $color1);
	imageline($image, $a1, $b1, $a2, $b2, $color2);
	imageline($image, $m2, $n2, $m1, $n1, $color3);
	imageline($image, $m2+5, $n2+5, $m1+5, $n1+5, $color4);
	imageline($image, $a1+10, $b1+10, $a2+10, $b2+10, $color2);
	
	return $image;
}
function checkCode($rCode){
	if(empty($rCode)||empty($_SESSION[CODE_MARK])){
		return false;
	}
	$inCode = $_SESSION[CODE_MARK];
	unset($_SESSION[CODE_MARK]);
	return $rCode == $inCode;
}